package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.CoachSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Coach;
import com.club.football.mapper.CoachMapper;
import com.club.football.req.CoachPageReq;
import com.club.football.service.CoachService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.R;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.club.football.common.CommonConstants.EDIT_OK_MSG;
import static com.club.football.common.CommonConstants.SAVE_OK_MSG;

/**
 * 实施教练服务
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements CoachService {
    @Override
    public R<String> coachSave(CoachSaveDto saveDto) {
        Coach coach = BeanUtils.copier(saveDto, new Coach());
        Long userId = UserHolder.getUser().getUserId();
        coach.setUpdateBy(userId);
        if (null != saveDto.getCoachId()) {
            updateById(coach);
            return R.ok(EDIT_OK_MSG);
        }
        coach.setCreateBy(userId);
        save(coach);
        return R.ok(SAVE_OK_MSG);
    }

    @Override
    public R<PageDto<Coach>> coachPage(PageDto<Coach> pageDto, CoachPageReq coachPageReq) {
        lambdaQuery().like(StringUtils.isNotBlank(coachPageReq.getName()), Coach::getName, coachPageReq.getName())
                .eq(coachPageReq.getTeamId() != null, Coach::getTeamId, coachPageReq.getTeamId())
                .page(pageDto);
        List<Coach> records = pageDto.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            pageDto.setRecords(records);
        }
        return R.ok(pageDto);
    }
}
